﻿using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace MagickServer.Utils {
    public class ImageRoute : RouteBase {
        public ImageRoute(Regex source, string action, IDictionary<string, string> parameters) {
            Source = source;
            Action = action;
            Parameters = parameters;
        }

        public Regex Source { get; private set; }
        public string Action { get; private set; }
        public IDictionary<string, string> Parameters { get; private set; }

        public string GetMatch(HttpContextBase httpContext) {
            if (Source.IsMatch(httpContext.Request.RawUrl))
                return httpContext.Request.RawUrl;
            return null;
        }

        public override RouteData GetRouteData(HttpContextBase httpContext) {
            var match = GetMatch(httpContext);
            if (match == null)
                return null;
            var data = new RouteData(this, new MvcRouteHandler());
            data.Values["controller"] = "Image";
            data.Values["action"] = Source.Replace(match, Action);
            foreach (var p in Parameters)
                data.Values[p.Key] = Source.Replace(match, p.Value);

            return data;
        }

        public override VirtualPathData GetVirtualPath(RequestContext requestContext, RouteValueDictionary values) {
            throw new NotImplementedException();
        }
    }
}